package com.nutransit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;

import com.mylibrary.mylibrary.parsing.AsyncTaskInterface;
import com.mylibrary.mylibrary.parsing.AsyncTaskUtility;
import com.nutransit.commoncode.MySpinnerAdapter;
import com.nutransit.commoncode.Webservice_parametrs;

public class Fragment_van_route_selection extends Fragment implements Webservice_parametrs
{
	private View rootview;
	private Spinner spn_choose_borough, spn_choose_Route;
	
	private Context context;
	private Resources resources;
	private MySpinnerAdapter mySpinnerAdapter_nucity, mySpinnerAdapter_nuvan;
	
	private String str_CID, str_VID = "0";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		rootview = inflater.inflate(R.layout.fragment_van_selection, container,false);
		
		init_var();
		
		get_data();
		return rootview;
	}

	private void init_var()
	{
		context = getActivity();
		resources = context.getResources();
		
		spn_choose_borough = (Spinner) rootview.findViewById(R.id.spn_choose_borough);
		spn_choose_Route = (Spinner) rootview.findViewById(R.id.spn_choose_Route);
		
		(rootview.findViewById(R.id.btn_Send)).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				Bundle bundle = new Bundle();
				bundle.putString(TAG_vid, str_VID);
				Fragment_Van_Root_Details fragment_Van_Root_Details = new Fragment_Van_Root_Details();
				fragment_Van_Root_Details.setArguments(bundle);
				final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
				ft.add(R.id.fLayout_Container, fragment_Van_Root_Details, "MyFragmentTag");
				ft.addToBackStack(null);
				ft.commit();
			}
		});
	}
	
	private void get_data()
	{
		new AsyncTaskUtility(context, null, URL_GET_NU_CITY_VAN).getJSONResponse(new AsyncTaskInterface()
		{
			@Override
			public void getJSONObjectFromAsynkTask(JSONObject json)
			{
				try
				{
					if(json!=null && json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
					{
						final JSONArray jsonArray_nucity = json.getJSONArray(TAG_nucity); 
						mySpinnerAdapter_nucity = new MySpinnerAdapter(context, jsonArray_nucity);
						spn_choose_borough.setAdapter(mySpinnerAdapter_nucity);
						
						final JSONArray jsonArray_nuvan = json.getJSONArray(TAG_nuvan); 
						mySpinnerAdapter_nuvan = new MySpinnerAdapter(context, jsonArray_nuvan);
						spn_choose_Route.setAdapter(mySpinnerAdapter_nuvan);
						
						spn_choose_borough.setOnItemSelectedListener(new OnItemSelectedListener()
						{
							@Override
							public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
							{
								try
								{
									str_CID = ((TextView)view.findViewById(R.id.tv_id)).getText().toString().trim();
									JSONArray jsonArray = new JSONArray();
									for (int i = 0; i < jsonArray_nuvan.length(); i++)
									{
										if(jsonArray_nuvan.getJSONObject(i).getString(TAG_vcid).equalsIgnoreCase(str_CID))
										{
											jsonArray.put(jsonArray_nuvan.getJSONObject(i));
										}
									}
									set_State_Spinner_Value(jsonArray, spn_choose_Route, context);
								}
								catch(JSONException e)
								{
									e.printStackTrace();
								}
							}
							@Override public void onNothingSelected(AdapterView<?> parent) { }
						});
						spn_choose_Route.setOnItemSelectedListener(new OnItemSelectedListener()
						{
							@Override
							public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
							{
								System.out.println("view: "+view);
								if(view!=null)
								{
									TextView tv_Id = (TextView)view.findViewById(R.id.tv_id);
									System.out.println("tv_Id: "+tv_Id);
									if(tv_Id!=null)
										str_VID = tv_Id.getText().toString().trim();
								}
							}
							@Override public void onNothingSelected(AdapterView<?> parent) { }
						});
					}
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	private void set_State_Spinner_Value(JSONArray jsonArray_State, Spinner spinner, Context context2)
	{
		if(jsonArray_State!=null  && spinner!=null)
		{
			mySpinnerAdapter_nuvan = new MySpinnerAdapter(context2, jsonArray_State);
			spinner.setAdapter(mySpinnerAdapter_nuvan);
		}
	}
}
